﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
using CreditAgricole.Framework;

namespace CreditAgricole.PageCodeBehind
{
    public class ITHome : ContextPage
    {
        #region Properties
        
        protected MepViewsWebPart ViewsWebpart;

        #endregion
        
        /// <summary>
        /// Load page event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            SelectMenuItem(Constants.ElementsSiteMenu.Activities);
            Guid webId = SelectMepWebId();
            if (webId != Guid.Empty)
            {
                ViewsWebpart.WebId = webId;
                ViewsWebpart.SiteId = SPContext.Current.Site.ID;
                if (!string.IsNullOrEmpty(Request.QueryString["selected"]))
                {
                    Constants.ElementsMenu selectedItem = (Constants.ElementsMenu)MappingTool.CatchIntNull(Request.QueryString["selected"]);
                    Constants.ElementsMenuCritiria selectedCritia = (Constants.ElementsMenuCritiria)MappingTool.CatchIntNull(Request.QueryString["critiria"]);
                    ViewsWebpart.ViewsMenu.SelectedMenu = selectedItem;
                    ViewsWebpart.ViewsMenu.SelectedCritiria = selectedCritia;
                }
                else
                {
                    ViewsWebpart.ViewsMenu.SelectedMenu = Constants.ElementsMenu.TasksToBeValidatedTag;
                }
            }
        }


        private Guid SelectMepWebId()
        {
            Guid webId = Guid.Empty;
            SPWeb mepWeb = (from web in SPContext.Current.Site.AllWebs.ToList()
                             where web.Lists.TryGetList(Constants.ChangingListTitle) != null
                             select web).FirstOrDefault();
            if (mepWeb != null)
            {
                webId = mepWeb.ID;
            }
            return webId;
        }
    }
}
